﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using GSM.BusinessLayer;

using GSM.BusinessLayer.DataLayer;

namespace CRM_SMS_MARKETING.PL.Customer_Care
{
    public partial class frmPromotion : DevExpress.XtraEditors.XtraForm
    {
        bool Adding;
        bool Editing;
        List<Promotion> ListPromotion;
        PromotionSql promotionsql;
        public frmPromotion()
        {
            InitializeComponent();
            SetReadOnly(true, true);
            promotionsql = new PromotionSql();
            ListPromotion = new List<Promotion>();
        }
        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (gridViewPromotion.RowCount != 0)
            {
                Editing = true;
                txtContent.Properties.ReadOnly = false;
                txtPromotionSubject.Properties.ReadOnly = false;
                txtType.Properties.ReadOnly = false;
                EnableControl(false, true);
                gridControlPromotion.Enabled = false;
                btnEdit.Enabled = false;
            }

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            bool checkadd = true;
            bool checkedit = true;

            bool check = dxValidationProvider1.Validate();
            if (check)
            {
                if (Adding == true)
                {
                    Promotion pro = new Promotion();

                    #region Mapping field
                    pro.PromotionID = Int32.Parse(txtID.Text);
                    pro.PromotionSubject = txtPromotionSubject.Text;
                    pro.PromotionContent = txtContent.Text;
                    pro.Type = txtType.Text;

                    #endregion
                    try
                    {
                        promotionsql.Insert(pro);
                        checkadd = true;
                    }
                    catch (Exception ex)
                    {
                        checkadd = false;
                        MessageBox.Show(ex.Message);
                    }
                    if (checkadd == true)
                    {
                        MessageBox.Show("Add Promotion Successfully!");
                        RefreshGridPromotion();
                        //Turn off mode Adding
                        #region  Enabled Control
                        Adding = false;
                        btnSave.Enabled = false;
                        btnCancel.Enabled = false;
                        btnNew.Enabled = true;
                        btnDelete.Enabled = true;
                        btnEdit.Enabled = true;
                        btnNext.Enabled = true;
                        btnPrevious.Enabled = true;
                        gridControlPromotion.Enabled = true;
                        #endregion
                    }
                    else
                    {
                        MessageBox.Show("Add Promotion Fail!");
                    }
                }
                if (Editing == true)
                {
                    Promotion pro = new Promotion();
                    #region Mapping field
                    pro.PromotionID = Int32.Parse(txtID.Text);
                    pro.PromotionSubject = txtPromotionSubject.Text;
                    pro.PromotionContent = txtContent.Text;
                    pro.Type = txtType.Text;

                    #endregion
                    try
                    {
                        promotionsql.Update(pro);
                    }
                    catch (Exception ex)
                    {
                        checkedit = false;
                        MessageBox.Show(ex.Message);
                    }
                    if (checkedit == true)
                    {
                        MessageBox.Show("Edit Promotion Successfully!");
                        int i = gridViewPromotion.GetFocusedDataSourceRowIndex();
                        RefreshGridPromotion();

                        gridViewPromotion.MoveBy(i);
                        //Turn off mode Editing
                        #region  Enabled Control
                        Editing = false;
                        btnSave.Enabled = false;
                        btnCancel.Enabled = false;
                        btnNew.Enabled = true;
                        btnDelete.Enabled = true;
                        btnEdit.Enabled = true;
                        btnNext.Enabled = true;
                        btnPrevious.Enabled = true;
                        gridControlPromotion.Enabled = true;
                        #endregion
                    }
                    else
                    {
                        MessageBox.Show("Edit Promotion Fail!");
                    }

                }
            }
            else
            {
                MessageBox.Show("Some field are not allowed null or blank value");
            }

        }
        int checkFirst = 1;
        private void btnPrevious_Click(object sender, EventArgs e)
        {
            gridViewPromotion.MovePrev();
            if (gridViewPromotion.IsFirstRow)
            {
                gridViewPromotion.MoveFirst();
                checkFirst++;
                if (checkFirst % 2 == 1)
                {
                    gridViewPromotion.MoveLast();
                }
            }

        }
        int checklast = 1;
        private void btnNext_Click(object sender, EventArgs e)
        {

            gridViewPromotion.MoveNext();
            if (gridViewPromotion.IsLastRow)
            {
                gridViewPromotion.MoveLast();
                checklast++;
                if (checklast % 2 == 1)
                {
                    gridViewPromotion.MoveFirst();
                }
            }



        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (gridViewPromotion.RowCount != 0)
            {

                #region Enabled Control
                btnNew.Enabled = true;
                btnEdit.Enabled = true;
                btnDelete.Enabled = true;
                btnNext.Enabled = true;
                btnPrevious.Enabled = true;
                btnSave.Enabled = false;
                btnCancel.Enabled = false;
                gridControlPromotion.Enabled = true;
                #endregion
                SetReadOnly(true, true);
                try
                {
                    PromotionKeys keys = new PromotionKeys((int)(gridViewPromotion.GetFocusedRowCellValue(colID)));
                    Promotion pro = new Promotion();
                    pro = promotionsql.SelectByPrimaryKey(keys);
                    MappingField(pro);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (gridViewPromotion.RowCount != 0)
            {
                bool checkdeleted = true;
                try
                {
                    Promotion pro = new Promotion();
                    #region Mapping field
                    pro.PromotionID = Int32.Parse(txtID.Text);
                    pro.PromotionSubject = txtPromotionSubject.Text;
                    pro.PromotionContent = txtContent.Text;
                    pro.Type = txtType.Text;
                    pro.Deleted = true;

                    #endregion
                    promotionsql.Update(pro);


                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    checkdeleted = false;
                }
                if (checkdeleted)
                {
                    MessageBox.Show("Delete Promotion Successfully!");
                }
                gridViewPromotion.MovePrev();
                RefreshGridPromotion();
            }
        }
        void SetReadOnly(bool value, bool value2)
        {
            txtID.Properties.ReadOnly = value;
            txtContent.Properties.ReadOnly = value;
            txtPromotionSubject.Properties.ReadOnly = value;
            txtType.Properties.ReadOnly = value;
        }
        void RefreshGridPromotion()
        {
            try
            {
                ListPromotion = promotionsql.SelectByField("Deleted", 0);
                ListPromotion.RemoveAt(0);
                ListPromotion.RemoveAt(0);
                gridControlPromotion.DataSource = ListPromotion;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            try
            {
                PromotionKeys keys = new PromotionKeys((int)(gridViewPromotion.GetFocusedRowCellValue(colID)));
                Promotion pro = new Promotion();
                pro = promotionsql.SelectByPrimaryKey(keys);
                MappingField(pro);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
        private void btnNew_Click(object sender, EventArgs e)
        {
            txtID.Properties.ReadOnly = true;
            txtID.Text = ((int)promotionsql.GetLastReCordID() + 1).ToString();
            txtContent.Properties.ReadOnly = false;
            txtPromotionSubject.Properties.ReadOnly = false;
            txtType.Properties.ReadOnly = false;
            txtContent.Text = "";
            txtPromotionSubject.Text = "";
            txtType.Text = "";
            Adding = true;
            #region Enabled Button
            btnSave.Enabled = true;
            btnCancel.Enabled = true;
            btnDelete.Enabled = false;
            btnEdit.Enabled = false;
            btnNext.Enabled = false;
            btnPrevious.Enabled = false;
            gridControlPromotion.Enabled = false;
            #endregion

        }

        void EnableControl(bool value1, bool value2)
        {
            #region Enabled Control
            btnNew.Enabled = value1;
            btnEdit.Enabled = value2;
            btnDelete.Enabled = value1;
            btnNext.Enabled = value1;
            btnPrevious.Enabled = value1;
            btnSave.Enabled = value2;
            btnCancel.Enabled = value2;
            #endregion
        }

        private void frmPromotion_Load(object sender, EventArgs e)
        {
            RefreshGridPromotion();
            gridViewPromotion.MoveFirst();
        }

        void MappingField(Promotion pro)
        {
            txtID.Text = pro.PromotionID.ToString();
            txtContent.Text = pro.PromotionContent;
            txtPromotionSubject.Text = pro.PromotionSubject;
            txtType.Text = pro.Type;
        }

        private void gridViewPromotion_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            try
            {
                PromotionKeys keys = new PromotionKeys((int)(gridViewPromotion.GetRowCellValue(e.RowHandle, colID)));
                Promotion pro = new Promotion();
                pro = promotionsql.SelectByPrimaryKey(keys);
                MappingField(pro);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void gridViewPromotion_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            try
            {
                PromotionKeys keys = new PromotionKeys((int)(gridViewPromotion.GetRowCellValue(e.RowHandle, colID)));
                Promotion pro = new Promotion();
                pro = promotionsql.SelectByPrimaryKey(keys);
                MappingField(pro);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void gridViewPromotion_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (ListPromotion.Count == 0)
            {
                #region Clear textbox

                txtID.Text = "";
                txtContent.Text = "";
                txtPromotionSubject.Text = "";
                txtType.Text = "";

                #endregion
            }
            else
            {
                try
                {
                    PromotionKeys keys = new PromotionKeys((int)(gridViewPromotion.GetFocusedRowCellValue(colID)));
                    Promotion pro = new Promotion();
                    pro = promotionsql.SelectByPrimaryKey(keys);
                    MappingField(pro);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
        }

    }
}